package com.itheima.exer.exercise.ex0924;

import java.util.Scanner;

public class Demo6 {
    public static void main(String[] args) {
        int[] arr = {35, 60, 78, 90, 90, 99, 100};
        System.out.println("请输入您要查找的数据:");
        int number = new Scanner(System.in).nextInt();
        int index = getIndex(arr, number);
        if (index != -1) {
            System.out.println("您查找的数据在数组的第" + index + "个索引.");
        } else {
            System.out.println("您查找的数不在这个数组中!");
        }
    }

    private static int getIndex(int[] arr, int number) {
        if (number < arr[0] || number > arr[arr.length - 1]) {
            return -1;
        }
        int max = arr.length-1;
        int min = 0;
        while (max >= min) {
            int mid = (max + min ) >> 1;
            if (number == arr[mid]){
                return mid;
            }else if(number < arr[mid]){
                max = mid -1;
            }else if(number > arr[mid]){
                min = mid +1;
            }
        }
        return -1;
    }

}
